package org.example.admin.user;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class addUserView extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JTextField telField;
    private JTextField emailField;
    private JButton addButton;

    public addUserView() {
        setTitle("管理端用户信息添加页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(8, 2));

        JLabel usernameLabel = new JLabel("用户名");
        usernameField = new JTextField();
        JLabel passwordLabel = new JLabel("密码");
        passwordField = new JPasswordField();
        JLabel telLabel = new JLabel("电话");
        telField = new JTextField();
        JLabel emailLabel = new JLabel("邮箱");
        emailField = new JTextField();
        addButton = new JButton("添加");

        // 添加组件到窗口
        add(usernameLabel);
        add(usernameField);
        add(passwordLabel);
        add(passwordField);
        add(telLabel);
        add(telField);
        add(emailLabel);
        add(emailField);
        add(new JLabel()); // 占位
        add(addButton);

        // 添加添加按钮的点击事件监听器
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addUserInfoToDatabase();
            }
        });
    }

    private void addUserInfoToDatabase() {
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());
        String tel = telField.getText();
        String email = emailField.getText();

        try {
            // 连接数据库
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");

            // 创建插入语句
            String insertQuery = "INSERT INTO air_user (username, password, telephone, email) VALUES (?, ?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(insertQuery);
            statement.setString(1, username);
            statement.setString(2, password);
            statement.setString(3, tel);
            statement.setString(4, email);

            // 执行插入操作
            statement.executeUpdate();

            // 关闭连接和语句
            statement.close();
            conn.close();

            // 提示添加成功
            JOptionPane.showMessageDialog(this, "添加成功");

            // 清空输入框
            clearFields();

        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "添加失败：" + e.getMessage());
        }
    }

    private void clearFields() {
        usernameField.setText("");
        passwordField.setText("");
        telField.setText("");
        emailField.setText("");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            addUserView addUserViewForm = new addUserView();
            addUserViewForm.setSize(300, 200);
            addUserViewForm.setVisible(true);
        });
    }
}